package a66weding.com.jiehuntong.pager;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONException;

import java.util.HashMap;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.ToolsDayPlanActivity;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.WedPlanTypeBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.fragment.BaseFragment;
import a66weding.com.jiehuntong.fragment.IFragmentCallBack;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/6/3
 * @description :
 */
public class ToolsPager_DayPlan_editTask extends BaseFragment implements View.OnClickListener, OnNetResultListener {

    private ToolsDayPlanActivity mPlanActivity;
    private EditText mEditTaskEt;
    private ImageView mDeletask;
    private Button mCommit;
    private TextView mTitleTv;
    private TextView mBackTv;
    private TextView mHeadRightTv;
    private LinearLayout mBackLay;

    private List<WedPlanTypeBean.DataBean> mTypeList;
    private int mCurrentGroupPosition;
    private String weddingProcessClassName;
    private String content;
    private static IFragmentCallBack callBack;



    public static ToolsPager_DayPlan_editTask newInstance(IFragmentCallBack callBack2) {
        ToolsPager_DayPlan_editTask frag = new ToolsPager_DayPlan_editTask();
        callBack=callBack2;
        return frag;
    }


    @Override
    public View initView() {
        mPlanActivity=  (ToolsDayPlanActivity) getActivity();

        View mEditView = View.inflate(mActivity, R.layout.pager_tools_dayplan_edittask, null);
        mEditTaskEt = (EditText) mEditView.findViewById(R.id.et_tools_dayplan_editdelete);
        mDeletask = (ImageView) mEditView.findViewById(R.id.iv_tools_delete);
        mCommit = (Button) mEditView.findViewById(R.id.btn_tools_dayplan_editcommit);
        mHeadRightTv = (TextView) mEditView.findViewById(R.id.tv_tools_dayplan_addtype1);



        mTitleTv = (TextView) mEditView.findViewById(R.id.tv_tools_dayplan_title);
        mBackTv = (TextView) mEditView.findViewById(R.id.tv_tools_dayplan_backcontent);
        mBackLay = (LinearLayout) mEditView.findViewById(R.id.llay_toolsheader_back);

        mHeadRightTv.setVisibility(View.GONE);

        mTitleTv.setText("修改流程类型");
        mBackTv.setText("返回");

        mDeletask.setOnClickListener(this);
        mCommit.setOnClickListener(this);
        mBackLay.setOnClickListener(this);


        return mEditView;

    }

    @Override
    public void initData() {

        mTypeList = mPlanActivity.getmTypeList();

        mCurrentGroupPosition = mPlanActivity.getmCurrentGroupPosition();
        weddingProcessClassName = mTypeList.get(mCurrentGroupPosition).getWeddingProcessClassName();
        mEditTaskEt.setText(weddingProcessClassName);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
                case R.id.iv_tools_delete:
                    mEditTaskEt.setText("");
                break;
                 case R.id.btn_tools_dayplan_editcommit:

                     content = mEditTaskEt.getText().toString().trim();
                     if(TextUtils.isEmpty(content)) {
                         CUtils.showToast("类型名称不能为空！");
                         return;
                     }

                     //向服务器发送数据
                     String url= UrlDealUtil.getRealUrl(RequestCode.UPDATEWEDDINGPROCESSCLASS);

                     HashMap<String,String> paramsMap =new HashMap<>();
                     int weddingProcessClassID = mTypeList.get(mCurrentGroupPosition).getWeddingProcessClassID();
                     paramsMap.put("WeddingProcessClassID",""+weddingProcessClassID);
                     paramsMap.put("WeddingProcessClassName", content);

                     mTypeList.get(mCurrentGroupPosition).setWeddingProcessClassName(content);



                     HttpRequestBean editTypeHttp=new HttpRequestBean(RequestCode.POSTEQUEST,url,this,
                             CommonResultBean.class,RequestCode.UPDATEWEDDINGPROCESSCLASS,paramsMap);

                     CUtils.showToast("请求网络数据");

                     HttpHandleUtil.executeCommonPost(mPlanActivity,editTypeHttp);

                     showCommitPopupWindow(v);


                     break;

                case R.id.llay_toolsheader_back:
                    mPlanActivity.getSupportFragmentManager().popBackStack();
                    break;
        }
    }



    @Override
    public void onSucess(SuccessBean sucessbean) {
        CommonResultBean data = (CommonResultBean) sucessbean.getData();
        if(mCommitpop!=null) {
            mCommitpop.dismiss();
        }
        switch (data.getResCode()) {
                case "0000":
                    CUtils.showToast("修改类型成功");
                    mTypeList.get(mCurrentGroupPosition).setWeddingProcessClassName(content);
                    mPlanActivity.setmTypeList(mTypeList);
                    mPlanActivity.notifyData("任务流程主页");
                    mPlanActivity.notifyData("添加任务流程");
                    mPlanActivity.getSupportFragmentManager().popBackStack();
                break;
                 case "0001":

                 case "0002":

                 case "0003":

                    CUtils.showToast("不好意思，修改类型失败，请重试");
                break;
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }
}
